home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_003 / cforth / forth.lex < prev    next >
Text File  |  1992-05-06  |  4KB  |  126 lines

  1. %{
  2. /* LEX input for FORTH input file scanner */
  3. /* 
  4.     Specifications are as follows:
  5.     This file must be run through "sed" to change 
  6.         yylex () {
  7.     to
  8.         TOKEN *yylex () {
  9.     where the sed script is
  10.         sed "s/yylex () {/TOKEN *yylex () {/" lex.yy.c
  11.  
  12.     Note that spaces have been included above so these lines won't be
  13.     mangled by sed; in actuality, the two blanks surrounding () are
  14.     removed.
  15.  
  16.     The function "yylex()" always returns a pointer to a structure:
  17.  
  18.         struct tokenrec {
  19.         int type;
  20.         char *text;
  21.         }
  22.         #define TOKEN struct tokenrec
  23.  
  24.     where the type is a hint as to the word's type:
  25.         DECIMAL for decimal literal        d+
  26.         OCTAL for octal literal        0d*
  27.         HEX for hex literal        0xd+ or 0Xd+
  28.         C_BS for a literal Backspace    '\b'
  29.         C_FF for a literal Form Feed    '\f'
  30.         C_NL for a literal Newline    '\n'
  31.         C_CR for a literal Carriage Return '\r'
  32.         C_TAB for a literal Tab '\t'
  33.         C_BSLASH for a literal backslash '\\'
  34.         C_IT for an other character literal 'x' where x is possibly '
  35.         STRING_LIT for a string literal (possibly containing \")
  36.         COMMENT for a left-parenthesis (possibly beginning a comment)
  37.         PRIM for "PRIM"
  38.         CONST for "CONST"
  39.         VAR for "VAR"
  40.         USER for "USER"
  41.         LABEL for "LABEL"
  42.         COLON for ":"
  43.         SEMICOLON for ";"
  44.         SEMISTAR for ";*" (used to make words IMMEDIATE)
  45.         NUL for the token {NUL}, which gets compiled as a null byte;
  46.             this special interpretation takes place in the COLON
  47.             code.
  48.         LIT for the word "LIT" (treated like OTHER, except that
  49.             no warning is generated when a literal follows this)
  50.         OTHER for an other word not recognized above
  51.  
  52.     Note that this is just a hint: the meaning of any string of characters
  53.     depends on the context.
  54.  
  55. */
  56. %}
  57.  
  58. decimal    [0-9]
  59. hex    [0-9A-Fa-f]
  60. octal    [0-7]
  61. white    [ \t\n\r\f]
  62. tail    /{white}
  63.  
  64. %{
  65. #include "forth.lex.h"
  66. TOKEN token;
  67. %}
  68.  
  69. %%
  70. {white}*    /* whitespace -- keep looping */ ;
  71.  
  72. -?[1-9]{decimal}*{tail}        { token.type = DECIMAL; token.text = yytext;
  73.                     return &token; }
  74. -?0{octal}*{tail}        { token.type = OCTAL; token.text = yytext;
  75.                     return &token; }
  76. -?0[xX]{hex}+{tail}        { token.type = HEX; token.text = yytext;
  77.                     return &token; }
  78.  
  79. \'\\b\'{tail}    { token.type = C_BS; token.text = yytext; return &token; }
  80. \'\\f\'{tail}    { token.type = C_FF; token.text = yytext; return &token; }
  81. \'\\n\'{tail}    { token.type = C_NL; token.text = yytext; return &token; }
  82. \'\\r\'{tail}    { token.type = C_CR; token.text = yytext; return &token; }
  83. \'\\t\'{tail}    { token.type = C_TAB; token.text = yytext; return &token; }
  84. \'\\\\\'{tail}    { token.type = C_BSLASH; token.text = yytext; return &token; }
  85. \'.\'{tail}    { token.type = C_LIT; token.text = yytext; return &token; }
  86.  
  87. \"(\\\"|[^"])*\"{tail}    { token.type = STRING_LIT; token.text = yytext; 
  88.                 return &token; }
  89.  
  90. "("{tail}        { token.type = COMMENT; token.text = yytext;
  91.                 return &token; }
  92.  
  93. "PRIM"{tail}        { token.type = PRIM; token.text = yytext;
  94.                 return &token; }
  95.  
  96. "CONST"{tail}        { token.type = CONST; token.text = yytext;
  97.                 return &token; }
  98.  
  99. "VAR"{tail}        { token.type = VAR; token.text = yytext;
  100.                 return &token; }
  101.  
  102. "USER"{tail}        { token.type = USER; token.text = yytext;
  103.                 return &token; }
  104.  
  105. "LABEL"{tail}        { token.type = LABEL; token.text = yytext;
  106.                 return &token; }
  107.  
  108. ":"{tail}        { token.type = COLON; token.text = yytext;
  109.                 return &token; }
  110.  
  111. ";"{tail}        { token.type = SEMICOLON; token.text = yytext;
  112.                 return &token; }
  113.  
  114. ";*"{tail}        { token.type = SEMISTAR; token.text = yytext;
  115.                 return &token; }
  116.  
  117. "{NUL}"{tail}        { token.type = NUL; token.text = yytext;
  118.                 return &token; }
  119.  
  120. "LIT"{tail}        { token.type = LIT; token.text = yytext;
  121.                 return &token; }
  122.  
  123. [^ \n\t\r\f]+{tail}    { token.type = OTHER; token.text = yytext;
  124.                 return &token; }
  125. %%
  126.